/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.admin.api.entity.ControlLog;
import com.excesys.exsecs.admin.service.ControlLogService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务管理里系统操作日志管理
 *
 * @author luxinchao
 * @date 2023-03-14 14:55:29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/controllog")
@Tag(name = "日志管理-控制日志")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ControlLogController {

	private final ControlLogService controlLogService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param controlLog 系统操作日志
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('controllog_get')")
	public R getOperationLogPage(Page page, ControlLog controlLog) {
		if (ObjectUtil.isNotEmpty(controlLog) && null != controlLog.getCreateTime()) {
			LocalDateTime beginTime = controlLog.getCreateTime();
			LocalDateTime endTime = controlLog.getUpdateTime();
			controlLog.setCreateTime(null);
			controlLog.setUpdateTime(null);
			return R.ok(controlLogService.page(page, Wrappers.query(controlLog).lambda()
					.ge(ControlLog::getCreateTime, beginTime).lt(ControlLog::getCreateTime, endTime)
					.like(StringUtils.isNotBlank(controlLog.getOperRecord()), ControlLog::getOperRecord, controlLog.getOperRecord())));
		}

		return R.ok(controlLogService.page(page, Wrappers.query(controlLog)));
	}

	/**
	 * 通过id查询系统操作日志
	 * @param logId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{logId}")
	@PreAuthorize("@pms.hasPermission('controllog_get')")
	public R getById(@PathVariable("logId") String logId) {
		return R.ok(controlLogService.getById(logId));
	}

	/**
	 * 通过id删除系统操作日志
	 * @param logId id
	 * @return R
	 */
	@Operation(summary = "通过id删除系统操作日志", description = "通过id删除系统操作日志")
	@SysLog("通过id删除系统操作日志")
	@DeleteMapping("/{logId}")
	@PreAuthorize("@pms.hasPermission('controllog_del')")
	public R removeById(@PathVariable String logId) {
		return R.ok(controlLogService.removeById(logId));
	}

	/**
	 * 导出excel 表格
	 * @return
	 */
	/*@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('bum_operationlog_export')")
	public List<OperationLogExcelVO> export(ControlLog controlLog) {
		return operationLogService.listOperationLogByWhere(operationLog);
	}*/

}
